package com.lagou.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class SpringConfig {
    /**
     * 定义并声明死信交换器，死信队列，死信绑定
     * 死信交换器，死信队列，死信绑定没有任何特殊之处，只是人为的指定它们作为死信交换器，死信队列，死信绑定使用
     */
    @Bean
    public DirectExchange myDeadExchange() {
        return new DirectExchange("MyDeadExchange", true, true, null);
    }
    @Bean
    public Queue myDeadQueue() {
        return new Queue("MyDeadQueue", true, false, true, null);
    }
    @Bean
    public Binding myDeadRoutingKey() {
        return new Binding("MyDeadQueue", Binding.DestinationType.QUEUE, "MyDeadExchange", "MyDeadRoutingKey", null);
    }

    /**
     * 定义交换器
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("MyDirectExchange", true, true, null);
    }

    /**
     * 定义队列
     */
    @Bean
    public Queue queue() {
        //队列参数
        Map<String, Object> arguments = new HashMap<>();
        /**
         * 队列级别的消息生存时间
         */
        arguments.put("x-message-ttl", 10000);
        /**
         * 指定死信交换器
         */
        arguments.put("x-dead-letter-exchange", "MyDeadExchange");
        /**
         * 指定死信绑定
         */
        arguments.put("x-dead-letter-routing-key", "MyDeadRoutingKey");
        return new Queue("MyQueue", true, false, true, arguments);
    }

    /**
     * 定义绑定
     */
    @Bean
    public Binding binding() {
        return new Binding("MyQueue", Binding.DestinationType.QUEUE, "MyDirectExchange", "MyBindingKey", null);
    }
}
